package com.luxx.service;

import com.luxx.dao.AdminDao;
import com.luxx.dao.UserDao;
import com.luxx.entity.Admin;
import com.luxx.entity.User;
import com.luxx.response.Result;
import com.luxx.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * 登录业务
 *
 * @author luxx
 */
@Service
public class LoginService {

    public Result checkPassword(HttpServletRequest request, long id, String password) {

        User user = userDao.findOne(id);
        if (user == null) {
            return ResultUtil.error("账号错误");
        } else if (password.equals(user.getPassword())) {

            HttpSession session = request.getSession();
            session.setAttribute("user", user);

            return ResultUtil.success();
        } else {
            return ResultUtil.error("密码错误");
        }
    }

    public Result checkPassword2(HttpServletRequest request, long id, String password) {

        Admin admin = adminDao.findOne(id);
        if (admin == null) {
            return ResultUtil.error("账号错误");
        } else if (password.equals(admin.getPassword())) {

            HttpSession session = request.getSession();
            session.setAttribute("admin", admin);

            return ResultUtil.success();
        } else {
            return ResultUtil.error("密码错误");
        }
    }


    @Autowired
    private UserDao userDao;
    @Autowired
    private AdminDao adminDao;
}
